package com.news.servlet;


import com.news.pojo.Adver;
import com.news.service.AdverService;
import com.news.service.impl.AdverServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

//广告控制
@WebServlet("/adver.do")
public class AdverServlet extends HttpServlet {
    private AdverService adverService = new AdverServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest request , HttpServletResponse response ) throws ServletException, IOException {
        try{
            //2.设置响应内容格式
            response.setContentType("text/html;charset=UTF-8");
            //3.控制请求路由
            String opr = request.getParameter("opr");
            switch (opr){
                case "add":{
                    add(request,response);
                    break;
                }
                case "getAll":{
                    getAll(request,response);
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
    public void getAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Adver> all = adverService.getAll();
        System.out.println(all);
        request.getSession().setAttribute("adverst",all);
        response.sendRedirect("/index.jsp");
    }

}
